﻿
namespace PmfWeb.Core.WebControls
{
    ///<summary>
    /// 扩展Repeater控件
    ///</summary>
    [System.ComponentModel.DefaultProperty("Text")]
    [System.Web.UI.ToolboxData("<{0}:Repeater ID=\"Repeater1\" runat=\"server\" EnableViewState=\"false\" ><HeaderTemplate></HeaderTemplate><ItemTemplate></ItemTemplate><FooterTemplate></FooterTemplate></{0}:Repeater>")]
    public class Repeater : System.Web.UI.WebControls.Repeater
    {
        private int _currentpage;
        private string _pagelink;
        private int _pagesize;
        private int _recordcount;
        private bool _isPaging = false;
        ///<summary>
        /// 当前页码
        ///</summary>
        [System.ComponentModel.Category("Data")]
        [System.ComponentModel.DefaultValue("1")]
        [System.ComponentModel.Description("当前页")]
        [System.ComponentModel.Bindable(true)]
        public virtual int CurrentPage
        {
            get
            {
                return _currentpage;
            }
            set
            {
                _currentpage = value;
            }
        }
        ///<summary>
        /// 是否分页
        ///</summary>
        [System.ComponentModel.Category("Data")]
        [System.ComponentModel.DefaultValue("false")]
        [System.ComponentModel.Description("是否分页")]
        [System.ComponentModel.Bindable(true)]
        public virtual bool IsPaging
        {
            get
            {
                return _isPaging;
            }
            set
            {
                _isPaging = value;
            }
        }
        ///<summary>
        /// 链接地址，如:http://www.taoka.cc/list.aspx
        ///</summary>
        [System.ComponentModel.Description("当前页链接")]
        [System.ComponentModel.DefaultValue("")]
        [System.ComponentModel.Bindable(true)]
        [System.ComponentModel.Category("Data")]
        public virtual string PageLink
        {
            get
            {
                return _pagelink;
            }
            set
            {
                _pagelink = value;
            }
        }

        ///<summary>
        /// 页大小，每页显示的记录数
        ///</summary>
        [System.ComponentModel.Description("每页显示记录数")]
        [System.ComponentModel.DefaultValue("1")]
        [System.ComponentModel.Bindable(true)]
        [System.ComponentModel.Category("Data")]
        public virtual int PageSize
        {
            get
            {
                return _pagesize;
            }
            set
            {
                _pagesize = value;
            }
        }

        ///<summary>
        /// 记录总数
        ///</summary>
        [System.ComponentModel.Description("记录总数")]
        [System.ComponentModel.Bindable(true)]
        [System.ComponentModel.Category("Data")]
        [System.ComponentModel.DefaultValue("1")]
        public virtual int RecordCount
        {
            get
            {
                return _recordcount;
            }
            set
            {
                _recordcount = value;
            }
        }

        /// <summary>
        /// 构造器
        /// </summary>
        public Repeater()
        {
            _recordcount = 0;
            _pagesize = 0;
            _pagelink = "";
            _currentpage = 1;
        }

        /// <summary>
        /// 分页函数
        /// </summary>
        /// <param name="recordcount">总记录数</param>
        /// <param name="pagesize">每页记录数</param>
        /// <param name="currentpage">当前页数</param>
        /// <param name="url">Url参数</param>
        /// <returns></returns>
        public virtual string Pagination(int recordcount, int pagesize, int currentpage, string url)
        {
            int allcurrentpage = 0;
            int next = 0;
            int pre = 0;
            int startcount = 0;
            int endcount = 0;
            string currentpagestr = "";

            if (currentpage < 1) currentpage = 1;
            //计算总页数
            if (pagesize != 0)
            {
                allcurrentpage = (recordcount / pagesize);
                allcurrentpage = ((recordcount % pagesize) != 0 ? allcurrentpage + 1 : allcurrentpage);
                allcurrentpage = (allcurrentpage == 0 ? 1 : allcurrentpage);
            }
            next = currentpage + 1;
            pre = currentpage - 1;
            startcount = (currentpage + 5) > allcurrentpage ? allcurrentpage - 9 : currentpage - 4;//中间页起始序号
            //中间页终止序号
            endcount = currentpage < 5 ? 10 : currentpage + 5;
            //为了避免输出的时候产生负数，设置如果小于1就从序号1开始
            if (startcount < 1) { startcount = 1; }
            //页码+5的可能性就会产生最终输出序号大于总页码，那么就要将其控制在页码数之内
            if (allcurrentpage < endcount) { endcount = allcurrentpage; }
            currentpagestr = "总页数：" + allcurrentpage + "&nbsp;&nbsp;&nbsp;当前页：" + currentpage + "&nbsp;&nbsp;&nbsp;每页" + pagesize + "条记录&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

            currentpagestr += currentpage > 1 ? "<a href=\"" + url + "?page=1\">首页</a>&nbsp;&nbsp;<a href=\"" + url + "?page=" + pre + "\">上一页</a>" : "首页 上一页";
            //中间页处理
            for (int i = startcount; i <= endcount; i++)
            {
                currentpagestr += currentpage == i ? "&nbsp;&nbsp;<font color=\"#ff0000\">" + i + "</font>" : "&nbsp;&nbsp;<a href=\"" + url + "?page=" + i + "\">" + i + "</a>";
            }
            currentpagestr += currentpage != allcurrentpage ? "&nbsp;&nbsp;<a href=\"" + url + "?page=" + next + "\">下一页</a>&nbsp;&nbsp;<a href=\"" + url + "?page=" + allcurrentpage + "\">末页</a>" : " 下一页 末页";
            return currentpagestr;
        }

        /// <summary> 
        /// 输出html,在浏览器中显示控件
        /// </summary>
        /// <param name="output"> 要写出到的HTML</param>
        protected override void Render(System.Web.UI.HtmlTextWriter output)
        {
            base.Render(output);
            if(_isPaging)
                output.WriteLine("<div style=\"font-size:15px;; color:Blue\">" + Pagination(_recordcount, _pagesize, _currentpage, _pagelink) + "</div>");
        }

    }
}
